/*
 * @Title Ibatis.java
 * @Description
 * @Copyright Copyright(c) 2018
 * @Company com.lizhen
 * @author lizhen
 * @version 0.0.1
 */
package com.lizhen.core.function.ibatis;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.lizhen.core.function.ibatis.entity.Person;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;

/**
 * @author lizhen
 */
public class Ibatis {

    private static final String FILE_PATH = "com/lizhen/core/function/ibatis/Ibatis/SqlMapConfig.xml";

    /**
     * @author lizhen
     */
    public Ibatis() {
    }

    /**
     * @author lizhen
     */
    public static void main(String[] args) {
        Ibatis ibatis = new Ibatis();
        ibatis.insert();
    }

    /**
     * @author lizhen
     */
    private void insert() {
        try {
            Reader reader = Resources.getResourceAsReader(FILE_PATH);
            SqlMapClient sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);

            /* This would insert one record in Employee table. */
            System.out.println("Going to insert record.....");

            Person person = new Person("小明", 88);
            sqlMapClient.insert("com.lizhen.core.function.ibatis.entity.Person.insert", person);

            System.out.println("Record Inserted Successfully ");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
